﻿using Microsoft.AspNetCore.Mvc;

namespace _11Net8AspNetCore_WebApis.Controller
{
    /// <summary>
    /// 2、路由绑定
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RouteBandController
    {
        /// <summary>
        /// 2.1 路由绑定 默认是FromRoute  name和入参一样
        /// name 可以自定义
        /// </summary>
        /// <param name="id1"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public string GetRoute([FromRoute(Name ="id")] int id1)
        {
            return id1.ToString();
        }

        /// <summary>
        /// 2.2 请求参数绑定
        /// 
        /// ？参数名=值 [FromQuery(Name = "路由参数名")] 变量  进行匹配
        /// 无特性变量 路径中存在变量，进行匹配 不存在变量请求参数变量
        /// </summary>
        /// <param name="id1"></param>
        /// <returns></returns>
        [HttpGet("route1/{id}")]
        public string GetRoute1([FromRoute(Name = "id")] int id1, int age)
        {
            return id1.ToString();
        }

        /// <summary>
        /// 请求路径 https://localhost:2587/api/RouteBand/route2/1?age=1&name=a
        /// </summary>
        /// <param name="id1"></param>
        /// <param name="age"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("route2/{id}")]
        public string GetRoute2([FromRoute(Name = "id")] int id1, int age, [FromQuery]string name)
        {
            return id1.ToString();
        }
    }
}
